package com.ido.otaclient.nordic;

import android.content.Context;

import com.ido.otaclient.base.BasePreference;
import com.ido.otaclient.env.AppEnv;

/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2020-02-28 17:21
 * @description
 */
public class NordicConfigPerfence extends BasePreference {

    private static final String NAME = "nordic_preference";

    private static final String TASK_NAME = "taskName";
    private static final String UPLOAD_TYPE = "uploadName";
    private static final String FIREWARE_NORMAL_NAME = "firewareNormalName";
    private static final String FIREWARE_NORMAL_VERSION = "firewareNormalVersion";
    private static final String FIREWARE_NORMAL_PATH = "filewareNormalPath";
    private static final String DEVICE_ID = "deviceId";
    /**
     * 升级配置
     */
    private NordicConfig mNordicConfig;

    private static volatile NordicConfigPerfence instance;

    private static Context getContext() {
        return AppEnv.instance().getContext();
    }

    public static NordicConfigPerfence getInstance() {
        if (instance == null) {
            synchronized (NordicConfigPerfence.class) {
                if (instance == null) {
                    instance = new NordicConfigPerfence();
                }
            }
        }
        return instance;
    }

    /**
     * 保存配置在本地
     * @param nordicConfig
     */
    public void saveUploadConfig(NordicConfig nordicConfig){
        if(nordicConfig == null){
            nordicConfig = new NordicConfig();
        }
        saveString(getContext(),NAME,TASK_NAME,nordicConfig.getTaskName());
        saveString(getContext(),NAME,UPLOAD_TYPE,nordicConfig.getUploadName());
        saveString(getContext(),NAME,FIREWARE_NORMAL_NAME,nordicConfig.getFirewareNormalName());
        saveString(getContext(),NAME,FIREWARE_NORMAL_VERSION,nordicConfig.getFirewareNormalVersion());
        saveString(getContext(),NAME,FIREWARE_NORMAL_PATH,nordicConfig.getFirewareWarePath());
        saveString(getContext(),NAME,DEVICE_ID,nordicConfig.getDeviceId());
        mNordicConfig = nordicConfig;
    }

    /**
     * 从本地取出数据
     * @return
     */
    public NordicConfig getNordicConfig(){
        mNordicConfig = new NordicConfig();
        mNordicConfig.setTaskName(getString(getContext(),NAME,TASK_NAME));
        mNordicConfig.setUploadName(getString(getContext(),NAME,UPLOAD_TYPE));
        mNordicConfig.setFirewareNormalName(getString(getContext(),NAME,FIREWARE_NORMAL_NAME));
        mNordicConfig.setFirewareNormalVersion(getString(getContext(),NAME,FIREWARE_NORMAL_VERSION));
        mNordicConfig.setFirewareWarePath(getString(getContext(),NAME,FIREWARE_NORMAL_PATH));
        mNordicConfig.setDeviceId(getString(getContext(),NAME,DEVICE_ID));
        return mNordicConfig;
    }
}
